package net.hubt.itc.data;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class TimeTableXMLParser extends BasicXMLParser {
	static final String KEY_DETAIL = "detail";
	static final String KEY_MA_SV = "MSV";// parent node
	static final String KEY_LOP = "LOP";
	static final String KEY_THU = "thu";
	static final String KEY_CA = "ca";
	static final String KEY_MONHOC = "mon";
	static final String KEY_PHONGHOC = "phong";
	// static final String KEY_SINH_VIEN="SINHVIEN";
	private Element mRootNode;

	public void setRoot(Element root) {
		mRootNode = root;		
	}

	public String getMsv() {
		return getTagValueLevel1(KEY_MA_SV, mRootNode);
	}

	public String getLop() {
		return getTagValueLevel1(KEY_LOP, mRootNode);
	}

	public TimeTableList parseXmlData() {
		TimeTableList timetableList = null;
		if (mRootNode != null) {
			if (mRootNode.getChildNodes().getLength() > 0) {
				// only one student name with a list of transcript data
				timetableList = new TimeTableList();

				//scheduleDataList.setTenSinhVien(getTenSinhVien());
				timetableList.setMaSv((getMsv()));
				timetableList.setLop((getLop()));

				NodeList nlLichHoc = mRootNode
						.getElementsByTagName(KEY_DETAIL);
				if (nlLichHoc.getLength() > 0) {
					// found <MONHOC>
					int cntLichHoc = nlLichHoc.getLength();
					// extract every single <DETAIL> element tag
					for (int i = 0; i < cntLichHoc; i++) {
						// a single <LICHHOC> element tag
						Node nodeLichHoc = nlLichHoc.item(i);
						if (nodeLichHoc.getNodeType() == Node.ELEMENT_NODE) {
							Element element = (Element) nodeLichHoc;

							TimeTableData timetableDataItem = new TimeTableData();
							timetableDataItem.setThu(getTagValue(
									KEY_THU, element));
							timetableDataItem.setCa(getTagValue(KEY_CA,
									element));
							timetableDataItem.setMonHoc(getTagValue(
									KEY_MONHOC, element));
							timetableDataItem.setPhongHoc(getTagValue(KEY_PHONGHOC,
									element));
							timetableList.add(timetableDataItem);
						}
					}
				}
			}
		}
		return timetableList;
	}
}
